package com.block.product.service.union;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 零用够
 */
@Service
public class LingYongGouProductServiceImpl implements IUnionProductService {

    private static Logger log = LogFactory.getCommLog();

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {
        return new ResponseVo(AppRspCodeConstant.SUCCESS);
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        String requestRegisterUrl = productInfo.getChannelRegisterUrl();

        HttpRequest httpRequest = HttpUtil.createPost(requestRegisterUrl);
        httpRequest.header("Content-Type", "application/json");

        String mobile = userInfoVo.getMobile();

        long timestamp = System.currentTimeMillis();


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("channelCode", productInfo.getChannelCode());
        paramMap.put("mobile", mobile);
        paramMap.put("softType", "lyz_h5");
        paramMap.put("timestamp", timestamp);

        List<String> keyList = JSON.parseArray(JSON.toJSONString(paramMap.keySet()), String.class);
        Collections.sort(keyList);
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : keyList) {
            stringBuilder.append(StrUtil.format("{}={}&", key, paramMap.get(key)));
        }
        stringBuilder.append(StrUtil.format("appKey={}&appSecret={}", productInfo.getChannelKey(), productInfo.getChannelSecret()));
        String sign = stringBuilder.toString();

        String md5Sign = SecureUtil.md5(sign).toLowerCase();
        paramMap.put("sign", md5Sign);

        paramMap.put("clientVersion", "h5_2.0.1");
        paramMap.put("apiVersion", "v1");

        httpRequest.body(JSON.toJSONString(paramMap));
        String resultStr = httpRequest.execute().body();
        log.info("【产品联登】【零用够】【注册】注册完成【result】{}【MATCH_USER_URL】{}【paramMap】{}",
                resultStr, requestRegisterUrl, JSON.toJSONString(paramMap));

        JSONObject jsonObject = JSON.parseObject(resultStr);

        String retCode = jsonObject.getString("retCode");
        String retMsg = jsonObject.getString("retMsg");

        if (ObjectUtil.equals("0000", retCode)) {
            JSONObject retData = jsonObject.getJSONObject("retData");
            String code = retData.getString("code");
            if (StrUtil.equals(code, "2")) {
                String skipUrl = retData.getString("skipUrl");
                JSONObject resultData = new JSONObject();
                resultData.put("productUrl", skipUrl);
                return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
            }
        }
        return new ResponseVo(AppRspCodeConstant.THRID_FAILED, retMsg, "");
    }

}
